ch = int(input())
stroka = str(ch)
if len(stroka) == 4:
    l = [stroka[0],stroka[1],stroka[2],stroka[3]]
    t1 = int(l[0])
    t2 = int(l[1])
    t3 = int(l[2])
    t4 = int(l[3])
    t5 = int(l[0]+l[1])
    t6 = int(l[1]+l[2])
    t7 = int(l[2]+l[3])
    t8 = int(l[0]+l[1]+l[2])
    t9 = int(l[1]+l[2]+l[3])
    m = [ch,t1,t2,t3,t4,t5,t6,t7,t8,t9]
    for i in range(10):
        for j in range(10):
            if m[i] < m[j]:
                m[i],m[j] = m[j],m[i]
    k1 = 0
    k2 = 0
    k3 = 0
    k4 = 0
    k5 = 0
    k6 = 0
    k7 = 0
    k8 = 0
    k9 = 0
    k10 = 0
    c1 = 0
    c2 = 0
    c3 = 0
    c4 = 0
    c5 = 0
    c6 = 0
    c7 = 0
    c8 = 0
    c9 = 0
    c10 = 0
    for n in range(ch // 2):
        z = n + 1
        if m[9] % z == 0:
            k10 = k10 + 1
        if m[8] % z == 0:
            k9 = k9 + 1
        if m[7] % z == 0:
            k8 = k8 + 1
        if m[6] % z == 0:
            k7 = k7 + 1
        if m[5] % z == 0:
            k6 = k6 + 1
        if m[4] % z == 0:
            k5 = k5 + 1
        if m[3] % z == 0:
            k4 = k4 + 1
        if m[2] % z == 0:
            k3 = k3 + 1
        if m[1] % z == 0:
            k2 = k2 + 1
        if m[0] % z == 0:
            k1 = k1 + 1
    if k10 == 1:
        c10 = 1
    if k9 == 2:
        c9 = 1
    if k8 == 2:
        c8 = 1
    if k7 == 2:
        c7 = 1
    if k6 == 2:
        c6 = 1
    if k5 == 2:
        c5 = 1
    if k4 == 2:
        c4 = 1
    if k3 == 2:
        c3 = 1
    if k2 == 2:
        c2 = 1
    if k1 == 2:
        c1 = 1
    if c10 == 1:
        print(m[9])
    elif c9 == 1:
        print(m[8])
    elif c8 == 1:
        print(m[7])
    elif c7 == 1:
        print(m[6])
    elif c6 == 1:
        print(m[5])
    elif c5 == 1:
        print(m[4])
    elif c4 == 1:
        print(m[3])
    elif c3 == 1:
        print(m[2])
    elif c2 == 1:
        print(m[1])
    elif c1 == 1:
        print(m[0])
    else:
        print(0)
else:
    print(0)